package com.hc.model.utils;

import java.util.ArrayList;

import com.hc.DTO.AnimalCategoryMasterDTO;
import com.hc.DTO.AnimalDTO;
import com.hc.DTO.AnimalImagesDTO;
import com.hc.common.ActionEvent;
import com.hc.constants.Constants;
import com.hc.controller.AnimalController;
import com.hc.controller.AnimalImagesController;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;

public class AnimalModel {
	public static AnimalModel instance;

	public static AnimalModel getInstance() {
		if (instance == null)
			instance = new AnimalModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {

		switch (e.action) {
		case Constants.GET_ANIMAL_BY_CATEGORY:
			Bundle category = (Bundle) e.bundle;
			int idCategory = category.getInt("idAnimal");
			ArrayList<AnimalDTO> arrImages = getAllAnimalByCategory(
					(Context) e.sender, idCategory);
			e.viewData = arrImages;
			AnimalController.getInstance().handleModelViewEvent(e);
			break;

		case Constants.GET_ANIMAL_BY_STRING:
			Bundle strSearch = (Bundle) e.bundle;
			String keysearch = strSearch.getString("keyseach");
			ArrayList<AnimalDTO> arrImgByKey = getAllAnimalByKeySearch(
					(Context) e.sender, keysearch);
			e.viewData = arrImgByKey;
			AnimalController.getInstance().handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}

	public ArrayList<AnimalDTO> getAllAnimalByCategory(Context context, int id) {
		ArrayList<AnimalDTO> arr = new ArrayList<AnimalDTO>();
		String sql = "SELECT * FROM animal where AnimalCategoryId =" + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalDTO dto = new AnimalDTO();
			dto.setId(c.getInt(c.getColumnIndex("IdAnimal")));
			dto.setAnimalImagesDTO(getImageById(context,
					c.getInt(c.getColumnIndex("IdAnimal"))));
			dto.setCommonName(c.getString(c.getColumnIndex("CommonName")));
			dto.setScientificName(c.getString(c
					.getColumnIndex("ScientificName")));
			dto.setRegionalName(c.getString(c.getColumnIndex("RegionalName")));
			dto.setwLPA(c.getString(c.getColumnIndex("WLPA")));

			dto.setiUCN(c.getString(c.getColumnIndex("IUCN")));
			dto.setcITES(c.getString(c.getColumnIndex("CITES")));
			dto.setSmallNote(c.getString(c.getColumnIndex("SmallNote")));
			dto.setTradeInfo(c.getString(c.getColumnIndex("TradeInfo")));
			dto.setMoreInfo(c.getString(c.getColumnIndex("MoreInfo")));

			dto.setTradeMapDesc(c.getString(c.getColumnIndex("TradeMapDesc")));
			dto.setTradeMapCoords(c.getString(c
					.getColumnIndex("TradeMapCoords")));
			dto.setContentGuid(c.getString(c.getColumnIndex("ContentGuid")));
			dto.setUserId(c.getString(c.getColumnIndex("UserId")));
			dto.setAnimalCategoryId(c.getInt(c
					.getColumnIndex("AnimalCategoryId")));
			arr.add(dto);

		}
		c.close();
		db.close();
		return arr;
	}

	public ArrayList<AnimalDTO> getAllAnimalByKeySearch(Context context,
			String keysearch) {
		ArrayList<AnimalDTO> arr = new ArrayList<AnimalDTO>();
		String sql = "SELECT * FROM animal where CommonName LIKE '%"
				+ keysearch + "%'";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalDTO dto = new AnimalDTO();
			dto.setId(c.getInt(c.getColumnIndex("IdAnimal")));
			dto.setAnimalImagesDTO(getImageById(context,
					c.getInt(c.getColumnIndex("IdAnimal"))));
			dto.setCommonName(c.getString(c.getColumnIndex("CommonName")));
			dto.setScientificName(c.getString(c
					.getColumnIndex("ScientificName")));
			dto.setRegionalName(c.getString(c.getColumnIndex("RegionalName")));
			dto.setwLPA(c.getString(c.getColumnIndex("WLPA")));

			dto.setiUCN(c.getString(c.getColumnIndex("IUCN")));
			dto.setcITES(c.getString(c.getColumnIndex("CITES")));
			dto.setSmallNote(c.getString(c.getColumnIndex("SmallNote")));
			dto.setTradeInfo(c.getString(c.getColumnIndex("TradeInfo")));
			dto.setMoreInfo(c.getString(c.getColumnIndex("MoreInfo")));

			dto.setTradeMapDesc(c.getString(c.getColumnIndex("TradeMapDesc")));
			dto.setTradeMapCoords(c.getString(c
					.getColumnIndex("TradeMapCoords")));
			dto.setContentGuid(c.getString(c.getColumnIndex("ContentGuid")));
			dto.setUserId(c.getString(c.getColumnIndex("UserId")));
			dto.setAnimalCategoryId(c.getInt(c
					.getColumnIndex("AnimalCategoryId")));
			arr.add(dto);

		}
		c.close();
		db.close();
		return arr;
	}

	public AnimalImagesDTO getImageById(Context context, int id) {
		AnimalImagesDTO dto = new AnimalImagesDTO();
		String sql = "SELECT * FROM animalimages where AnimalId = " + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			dto.setId(c.getInt(c.getColumnIndex("Id")));
			dto.setAnimalId(c.getInt(c.getColumnIndex("AnimalId")));
			dto.setImageUrl(c.getString(c.getColumnIndex("ImageUrl")));
			dto.setActualName(c.getString(c.getColumnIndex("ActualName")));
			dto.setDescription(c.getString(c.getColumnIndex("Description")));
			break;
		}
		c.close();
		db.close();
		return dto;
	}

	public AnimalDTO getAnimalById(Context context, int id) {
		AnimalDTO dto = new AnimalDTO();
		String sql = "SELECT * FROM animal where AnimalCategoryId =" + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			dto.setId(c.getInt(c.getColumnIndex("IdAnimal")));
			dto.setCommonName(c.getString(c.getColumnIndex("CommonName")));
			dto.setScientificName(c.getString(c
					.getColumnIndex("ScientificName")));
			dto.setRegionalName(c.getString(c.getColumnIndex("RegionalName")));
			dto.setwLPA(c.getString(c.getColumnIndex("WLPA")));

			dto.setiUCN(c.getString(c.getColumnIndex("IUCN")));
			dto.setcITES(c.getString(c.getColumnIndex("CITES")));
			dto.setSmallNote(c.getString(c.getColumnIndex("SmallNote")));
			dto.setTradeInfo(c.getString(c.getColumnIndex("TradeInfo")));
			dto.setMoreInfo(c.getString(c.getColumnIndex("MoreInfo")));

			dto.setTradeMapDesc(c.getString(c.getColumnIndex("TradeMapDesc")));
			dto.setTradeMapCoords(c.getString(c
					.getColumnIndex("TradeMapCoords")));
			dto.setContentGuid(c.getString(c.getColumnIndex("ContentGuid")));
			dto.setUserId(c.getString(c.getColumnIndex("UserId")));
			dto.setAnimalCategoryId(c.getInt(c
					.getColumnIndex("AnimalCategoryId")));

		}
		c.close();
		db.close();
		return dto;
	}

	public boolean getIdAnimal(Context context, int idA) {
		boolean flag = false;
		String sql = "SELECT * FROM animal where IdAnimal=" + idA;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertAnimal(AnimalDTO animal, Context context) {
		boolean flag = false;
		String DATABASE_TABLE = "animal";
		ContentValues values = new ContentValues();
		values.put("IdAnimal", animal.getId());
		values.put("CommonName", animal.getCommonName());
		values.put("ScientificName", animal.getScientificName());
		values.put("RegionalName", animal.getRegionalName());
		values.put("WLPA", animal.getwLPA());
		values.put("IUCN", animal.getiUCN());
		values.put("CITES", animal.getcITES());
		values.put("SmallNote", animal.getSmallNote());
		values.put("TradeInfo", animal.getTradeInfo());
		values.put("MoreInfo", animal.getMoreInfo());
		values.put("TradeMapDesc", animal.getTradeMapDesc());
		values.put("TradeMapCoords", animal.getTradeMapCoords());
		values.put("ContentGuid", animal.getContentGuid());
		values.put("UserId", animal.getUserId());
		values.put("AnimalCategoryId", animal.getAnimalCategoryId());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
		db.close();

		return flag;
	}

	public boolean updateAnimal(AnimalDTO animal, Context context) {
		boolean flag = false;
		// String sql = "UPDATE changelog SET Id=" + id;
		DBHelper db = new DBHelper(context);
		String where = "IdAnimal=" + animal.getId() + "";
		ContentValues values = new ContentValues();
		values.put("CommonName", animal.getCommonName());
		values.put("ScientificName", animal.getScientificName());
		values.put("RegionalName", animal.getRegionalName());
		values.put("WLPA", animal.getwLPA());
		values.put("IUCN", animal.getiUCN());
		values.put("CITES", animal.getcITES());
		values.put("SmallNote", animal.getSmallNote());
		values.put("TradeInfo", animal.getTradeInfo());
		values.put("MoreInfo", animal.getMoreInfo());
		values.put("TradeMapDesc", animal.getTradeMapDesc());
		values.put("TradeMapCoords", animal.getTradeMapCoords());
		values.put("ContentGuid", animal.getContentGuid());
		values.put("UserId", animal.getUserId());
		values.put("AnimalCategoryId", animal.getAnimalCategoryId());
		flag = db.ExcuteUpdate(null, "animal", values, where, null);
		db.close();
		return flag;
	}

	public void deleteAnimal(AnimalDTO animal, Context context) {
		String sql = "IdAnimal=" + animal.getArrIdRemove();
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = "animal";
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
